package com.vipheyue.huaxue.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.vipheyue.huaxue.R;
import com.vipheyue.huaxue.adapter.BaseRecyclerAdapter;
import com.vipheyue.huaxue.adapter.BaseRecyclerViewHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class BrowseMenuLeftFragment extends Fragment {
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<String> mdata = new ArrayList<String>();
    String[] array = {"资料共享", "经验共享", "培训区", "交流区"};
    private BaseRecyclerAdapter adapter;

    private  int checkPosito=0;
    public BrowseMenuLeftFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mdata = new ArrayList(Arrays.asList(array));
        View view = inflater.inflate(R.layout.fragment_browse_menu_left, container, false);
        ButterKnife.bind(this, view);
        initview();
        return view;
    }

    private void initview() {
        //        GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);//设置显示方式
//        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)//第三
//                .drawable(R.drawable.divider)//这里可以是图片
//                .size(10)
//                .build());

        adapter = new BaseRecyclerAdapter<String>(getActivity(), mdata, R.layout.simple_list_item) {
            @Override
            public void init(BaseRecyclerViewHolder holder, String bean, int position) {
                holder.setText(R.id.text1, bean);
                if (checkPosito == position) {
                    holder.setBG(R.id.text1, R.drawable.border_edittext_checked);
                    holder.setTextColor(R.id.text1, Color.parseColor("#FFFFFF"));
                }else {
                    holder.setBG(R.id.text1, R.drawable.border_edittext_normal);
                    holder.setTextColor(R.id.text1, Color.parseColor("#000000"));
                }
            }
        };
        adapter.setOnItemClickLinster(new BaseRecyclerAdapter.OnItemClickLinster() {
            @Override
            public void onItemClick(View view, int position) {
                checkPosito=position;
                adapter.notifyDataSetChanged();
                Fragment detailFragment = new BrowseMenuRightFragment();
                Bundle mBundle = new Bundle();
                mBundle.putInt("argInt",position);
                detailFragment.setArguments(mBundle);
                final FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.detail_container, detailFragment);
                fragmentTransaction.commit();
            }
        });
        recyclerView.setAdapter(adapter);//设置适配器


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
